//
//  XCSMSBaseViewController.m
//  HOLLASMSLoginKit
//
//  Created by Mars on 2019/10/10.
//  Copyright © 2019 mao PengLin. All rights reserved.
//

#import "XCSMSBaseViewController.h"

@interface XCSMSBaseViewController () <UIGestureRecognizerDelegate>

@end

@implementation XCSMSBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *itemButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 30, 30)];
    itemButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    NSString *prefix = [NSString stringWithFormat:@"CAMPROSMSKit.bundle/campro_assets/"];
    if (SMSIsArabic()) {
        [itemButton setImage:[UIImage imageNamed:[prefix stringByAppendingString:@"campro_arrow_right.png"]] forState:UIControlStateNormal];
    } else {
        [itemButton setImage:[UIImage imageNamed:[prefix stringByAppendingString:@"campro_arrow_left.png"]] forState:UIControlStateNormal];
    }
    [itemButton addTarget:self action:@selector(leftBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIView *leftCustomView = [[UIView alloc] initWithFrame: itemButton.frame];
    [leftCustomView addSubview:itemButton];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftCustomView];
    
    if ([XCSMSTheme sharedTheme].isAccountRetrive) {
        self.navigationItem.title = HWLocalizedString(@"retrieve_account");
    } else {
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
        self.navigationItem.title = [NSString stringWithFormat:@"%@ %@",HWLocalizedString(@"login_title") ,app_Name];
    }
    
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:MediumFont(16.0) ,NSForegroundColorAttributeName:[XCSMSTheme sharedTheme].navBarTitleTextColor}];
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}

- (void)leftBarButtonClick{
    if (self.navigationController) {
        if (![self.navigationController popViewControllerAnimated:YES]) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        }
    }else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if ([self.navigationController.viewControllers count] == 1) {
        return NO;
    }else{
        return YES;
    }
}

- (void)dealloc{
    NSLog(@"%@-------delloc-------", NSStringFromClass([self class]));
}

@end
